package com.zn.bitmapDisplay
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.utils.getDefinitionByName;

    public class BitmapDataUtil
    {
        public static function drawBitmapData(displayObject:DisplayObject):BitmapData
        {
            var bitmapData:BitmapData = new BitmapData(displayObject.width, displayObject.height, true, 0);
            bitmapData.draw(displayObject);

            var bitmapData2:BitmapData;
            var rect:Rectangle = bitmapData.getColorBoundsRect(0xFF000000, 0x000000, false);

            bitmapData2 = new BitmapData(rect.width, rect.height, true, 0);
            bitmapData2.draw(bitmapData, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, new Rectangle(0, 0, rect.width, rect.height), true);

            return bitmapData2;
        }

        public static function getBitmap(displayObject:DisplayObject):Bitmap
        {
            return new Bitmap(drawBitmapData(displayObject));
        }

		/**
		 *创建bitmapData，可以多图像绘制，用于纸娃娃系统 
		 * @param imageClassNameList
		 * @return 
		 * 
		 */
        public static function createBitmapDataByImages(imageClassNameList:Array):BitmapData
        {
            var classObj:Class;
            var className:String;
            var itemBitmapData:BitmapData;
            var bitmapData:BitmapData;
            for (var i:int = 0; i < imageClassNameList.length; i++)
            {
                className = imageClassNameList[i];
                classObj = Class(getDefinitionByName(className));
                itemBitmapData = new classObj();

                if (bitmapData == null)
                    bitmapData = itemBitmapData;
                else
                {
                    bitmapData.draw(itemBitmapData);
                    itemBitmapData.dispose();
                }
            }

            return bitmapData;
        }
    }
}